//
//  VVideoPickCell.m
//  VChatVideoClip
//
//  Created by huangbolun on 2017/10/9.
//  Copyright © 2017年 huangbolun. All rights reserved.
//

#import "VVideoPickCell.h"
#import "VVideoClipManger.h"

@interface VVideoPickCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation VVideoPickCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setVideoAsset:(PHAsset *)videoAsset{
    
    PHVideoRequestOptions *option = [PHVideoRequestOptions new];
    option.networkAccessAllowed = YES;
    option.progressHandler = ^(double progress, NSError * _Nullable error, BOOL * _Nonnull stop, NSDictionary * _Nullable info) {
        NSLog(@"video %lf - %@",progress,info);
    };
    [[PHCachingImageManager defaultManager] requestAVAssetForVideo:videoAsset options:option resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
           self.imageView.image = [[VVideoClipManger shareClipManger] getVideoPreViewImageWithAsset:(AVURLAsset *)asset second:2];
            Float64 durastion = CMTimeGetSeconds(asset.duration);
            self.timeLabel.text = [self secondsToTime:durastion];
        }];
    }];
}

- (NSString *)secondsToTime:(NSInteger)seconds{
    NSInteger second = seconds % 60;
    NSInteger min = seconds / 60;
    return [NSString stringWithFormat:@"%02ld:%02ld",min,second];
}

@end
